package filter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

public class watermark {
    public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, int color, int fontSize, int x, int y, OutputStream outputStream) {
        try {
            File _file = new File(targetImg);
            Image src = ImageIO.read(_file);
            int width = src.getWidth(null);
            int height = src.getHeight(null);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics graphics = image.createGraphics();
            graphics.drawImage(src, 0, 0, width, height, null);
            graphics.setColor(new Color(color));
            graphics.setFont(new Font(fontName, fontStyle, fontSize));
            graphics.drawString(pressText, width - fontSize - x, height - fontSize / 2 - y);
            graphics.dispose();
            // JPEGImageEncoder encoder = JPEGImageEncoder.createJPEGEncoder(outputStream);
            // encoder.encode(image);
            // JPEGImageEncoder代替方案，rt.jar是sun私有包，OpenJDK没有这东西。
            ImageIO.write(image, "jpg", outputStream);
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
